package com.listener;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.commons.Constants;

import vo.User;

/**
 *解决HttpSession超时销毁后被重复销毁的问题
 *
 */
@WebListener
public class HttpSessionLifeCycleListener implements HttpSessionListener {

    /**
     * Default constructor. 
     */
    public HttpSessionLifeCycleListener() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see HttpSessionListener#sessionCreated(HttpSessionEvent)
     */
    public void sessionCreated(HttpSessionEvent arg0)  { 
         // TODO Auto-generated method stub
    }

	/**
     * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
     */
    public void sessionDestroyed(HttpSessionEvent event)  { 
         // 获取ServletContext对象，然后将对应的HttpSession删除掉
    	HttpSession session=event.getSession();
    	ServletContext sc=session.getServletContext();
    	User user=(User)session.getAttribute(Constants.USER_SESSION_KEY);
    	sc.removeAttribute(user.getUserid()+"");
    }
	
}
